package checkers3d.presentation;

import java.awt.*;

/**
 * A visible GUI component (which inherits from GUIPictureBox) that exists as a
 * bitmapped image. When a click is received it is sent to an attached
 * eventManager (e.g. some class that implements IInputObserver).
 *
 * @author      Ruben Acuna
 */
public class GUIButton extends GUIPictureBox implements IInputObserver
{
    /**
    * An object that implements IInputObserver and will handle input events.
    */
    IInputObserver eventManager;

    /**
    * Class constructor that loads image data from a file, positions it at
    * some coordinates, and attaches an IInputObserver object.
    *
    * @param filename A relative path to a file containing an image.
    * @param position The position of this object relative to its container.
    * @param eventManager A class that implements IInputObserver and will handle
    *                     events for the button.
    * @see GUIPictureBox
    */
    public GUIButton(String filename, Point position,
                     IInputObserver eventManager)
    {
        super(filename, position);
        this.eventManager = eventManager;
    }

    /**
    * Class constructor that uses a preexisting BufferedImage for image data,
    * positions it at some coordinates, and attaches an IInputObserver object.
    *
    * @param resource   A BufferedImage that will be used for image data.
    * @param position   The position of this object relative to its container.
    * @param eventManager A class that implements IInputObserver and will handle
    *                     events for the button.
    * @see GUIPictureBox
    */    
    public GUIButton(RenderResourceBitmap resource, Point position,
                     IInputObserver eventManager)
    {
        super(resource, position);
        this.eventManager = eventManager;
    }

    /**
     * Sets the current event observer attached to this button.
     *
     * @param eventManager An observer to attach.
     */
    public void setObserver(IInputObserver eventManager)
    {
        this.eventManager = eventManager;
    }

    /**
    * Handles a click within a visible area by sending it to the attached
    * eventManager.
    *
    * @param  position  Point object containing coordinates relative to the
    *                   position of the click within the GUIButton.
    */
    public void onClick(Point position)
    {
        if(getRenderResource().containsPoint(position))
            eventManager.onClick(position);
    }

    /**
    * Handles a key press, when the window is in focus, by sending it to the
    * attached eventManager.
    *
    * @param  key       The character of the key that was pressed.
    * @see IInputObserver
    */
    public void onKeyPress(char key)
    {
        eventManager.onKeyPress(key);
    }

    /**
    * Returns a string representation of the GUIBUtton which contains
    * information about its GUIPictureBox and event manager.
    *
    * @return       A string representation of this GUIBUtton.
    */
    @Override
    public String toString()
    {
        return getClass().getName() + " -> [" + super.toString() + "], "
                                    + eventManager.toString();
    }
}